
package crayon.ui;
import crayon.fc.*;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.BasicStroke;
import javax.swing.JPanel;
import java.awt.Point;
import java.util.Vector;
import static java.lang.Math.*;
import java.awt.Cursor;
import java.awt.BasicStroke;
import javax.imageio.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.awt.geom.*;

public class Grapher extends JPanel {
    GestionnaireTracing gestionnaireTracing;
    
    
    static final BasicStroke dash = new BasicStroke(1, BasicStroke.CAP_ROUND,
						    BasicStroke.JOIN_ROUND,
						    1.f,
						    new float[] { 4.f, 4.f },
						    0.f);
	                                                   
    int W = 400;
    int H = 300;
    Cursor m_cursor;
    int CurrentTracing;
    SpecializedPoint P1,P2,P3;
    boolean drawtangeante;
    public Grapher(GestionnaireTracing g) {
	gestionnaireTracing=g;
	CurrentTracing=0;
	drawtangeante=false;
    }
	
  
		
    public Dimension getPreferredSize() { return new Dimension(W, H); }
	
    protected void paintComponent(Graphics g) {
	super.paintComponent(g);
	W = getWidth();
	H = getHeight();
	Graphics2D g2 = (Graphics2D)g;
	// background
	g2.setColor(Color.WHITE);
	g2.fillRect(0, 0, W, H);
	g2.setColor(Color.BLACK);
	// g2.setStroke(dash);

	for(Gestionnaire_Point gestionnaire_p : gestionnaireTracing.getTracings()){
	    Iterator itr = gestionnaire_p.m_Plist.iterator();
	    P1=null;
	    P2=null;
	    P3=null;
	    if(itr.hasNext()){
		P1 = (SpecializedPoint)itr.next();
		g2.drawRect(P1.point.x-Gestionnaire_Point.CubeDemiSize,P1.point.y-Gestionnaire_Point.CubeDemiSize,Gestionnaire_Point.CubeDemiSize*2,Gestionnaire_Point.CubeDemiSize*2);
		if(P1.ctrl1!=null){
		    g2.fillRect(P1.ctrl1.x-Gestionnaire_Point.CubeDemiSize/2,P1.ctrl1.y-Gestionnaire_Point.CubeDemiSize/2,Gestionnaire_Point.CubeDemiSize,Gestionnaire_Point.CubeDemiSize);
		    g2.fillRect(P1.point.x-(P1.ctrl1.x-P1.point.x)-Gestionnaire_Point.CubeDemiSize/2,
				P1.point.y-(P1.ctrl1.y-P1.point.y)-Gestionnaire_Point.CubeDemiSize/2,
				Gestionnaire_Point.CubeDemiSize,Gestionnaire_Point.CubeDemiSize);
		    g2.drawLine(P1.ctrl1.x,P1.ctrl1.y,P1.point.x-(P1.ctrl1.x-P1.point.x),P1.point.y-(P1.ctrl1.y-P1.point.y));
		    
		}
	    }
	    while(itr.hasNext()){
	        P2 = (SpecializedPoint)itr.next();
		g2.drawRect(P2.point.x-Gestionnaire_Point.CubeDemiSize,P2.point.y-Gestionnaire_Point.CubeDemiSize,Gestionnaire_Point.CubeDemiSize*2,Gestionnaire_Point.CubeDemiSize*2);
		drawcurvedline(P1,P2,g2);
		if(P2.ctrl1!=null){
		    g2.fillRect(P2.ctrl1.x-Gestionnaire_Point.CubeDemiSize/2,P2.ctrl1.y-Gestionnaire_Point.CubeDemiSize/2,Gestionnaire_Point.CubeDemiSize,Gestionnaire_Point.CubeDemiSize);
		    g2.fillRect(P2.point.x-(P2.ctrl1.x-P2.point.x)-Gestionnaire_Point.CubeDemiSize/2,
				P2.point.y-(P2.ctrl1.y-P2.point.y)-Gestionnaire_Point.CubeDemiSize/2,
				Gestionnaire_Point.CubeDemiSize,Gestionnaire_Point.CubeDemiSize);
		    g2.drawLine(P2.ctrl1.x,P2.ctrl1.y,P2.point.x-(P2.ctrl1.x-P2.point.x),P2.point.y-(P2.ctrl1.y-P2.point.y));
		}
		if(itr.hasNext())
		    P1=P2;
	    }
	    	
	    if (gestionnaire_p.closePoint1!=null&&gestionnaire_p.closePoint2!=null)// draw closin if specify
		drawcurvedline(gestionnaire_p.closePoint1,gestionnaire_p.closePoint2,g2);

	}
	
	
	if(gestionnaireTracing.currentPoint()!=null)
	    g2.fillRect(gestionnaireTracing.currentPoint().x-Gestionnaire_Point.CubeDemiSize,gestionnaireTracing.currentPoint().y-Gestionnaire_Point.CubeDemiSize,Gestionnaire_Point.CubeDemiSize*2,Gestionnaire_Point.CubeDemiSize*2);
	
	
    }
    
    
	
    public void update (){
	repaint();
    }
    

    public void setMoveCursor(){
	this.setCursor(new Cursor(Cursor.MOVE_CURSOR));
    }
    
    public void backtoNormalcursor(){
	setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }


    public void setCrossCursor(){
	this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
    }

    public void tangeante(){
	drawtangeante=!drawtangeante;
    }


    public void drawcurvedline(SpecializedPoint P1, SpecializedPoint P2 ,Graphics2D g2 ){
	if(P1.ctrl1!=null&&P2.ctrl2==null)
	    g2.draw(new QuadCurve2D.Double(new Double(Integer.toString(P1.point.x)),new Double(Integer.toString(P1.point.y)),
					   new Double(Integer.toString(P1.ctrl1.x)),new Double(Integer.toString(P1.ctrl1.y)),
					   new Double(Integer.toString(P2.point.x)),new Double(Integer.toString(P2.point.y))));
	else if(P1.ctrl1!=null && P2.ctrl2!=null)
	    g2.draw(new CubicCurve2D.Double(new Double(Integer.toString(P1.point.x)),new Double(Integer.toString(P1.point.y)),
					    new Double(Integer.toString(P1.ctrl1.x)),new Double(Integer.toString(P1.ctrl1.y)),
					    new Double(Integer.toString(P2.ctrl2.x)),new Double(Integer.toString(P2.ctrl2.y)),
					    new Double(Integer.toString(P2.point.x)),new Double(Integer.toString(P2.point.y))));
	
	else if(P1.ctrl1==null && P2.ctrl2!=null)
	    g2.draw(new QuadCurve2D.Double(new Double(Integer.toString(P1.point.x)),new Double(Integer.toString(P1.point.y)),
					   new Double(Integer.toString(P2.ctrl2.x)),new Double(Integer.toString(P2.ctrl2.y)),
					   new Double(Integer.toString(P2.point.x)),new Double(Integer.toString(P2.point.y))));
	else
	    g2.drawLine(P1.point.x,P1.point.y,P2.point.x,P2.point.y);
	
    }
	

}
